// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Interfaces/IHttpRequest.h"
#include "HotChunkJsonHttp.generated.h"


DECLARE_MULTICAST_DELEGATE_TwoParams(FJsonHttpComplete, FString, bool);

DECLARE_MULTICAST_DELEGATE_ThreeParams(FJsonHttpProgress, FHttpRequestPtr, int32, int32);

/**
 * 
 */
UCLASS()
class HOTCHUNK_API UHotChunkJsonHttp : public UObject
{
	GENERATED_BODY()

public:

	struct FHotChunkJsonHttpRequest
	{
		FHttpRequestPtr HttpRequest;
		FJsonHttpComplete JsonHttpComplete;
		FJsonHttpProgress JsonHttpProgress;
		FHotChunkJsonHttpRequest(FHttpRequestPtr InHttpRequest) : HttpRequest(InHttpRequest) {}
	};

public:

	static UHotChunkJsonHttp* Get();

	void RequireDownLoad(const FString& Url, const TBaseDelegate<void, FString, bool>& RequestComplete, const FHttpRequestProgressDelegate& RequestProgress);

	void OnRequestComplete(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool IsSucceeded);

	void OnRequestProgress(FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived);

protected:

	TMap<FString, TSharedPtr<FHotChunkJsonHttpRequest>> RequestingMap;

	//TMap<FString, FString> CompleteResultsMap;
};


#if 0

using FRequestComplete = TFunction<void(FHttpRequestPtr HttpRequest, FHttpResponsePtr HttpResponse, bool bSucceeded)>;

using FRequestProgress = TFunction<void(FHttpRequestPtr HttpRequest, int32 BytesSent, int32 BytesReceived)>;


struct FHotChunkJsonHttpRequest
{
	FHttpRequestPtr HttpRequest;
	FRequestComplete RequestComplete;
	FRequestProgress RequestProgress;
	FHotChunkJsonHttpRequest(FHttpRequestPtr InHttpRequest, const FRequestComplete& InRequestComplete, const FRequestProgress& InRequestProgress) : HttpRequest(InHttpRequest), RequestComplete(InRequestComplete), RequestProgress(InRequestProgress) {}
};

#endif